<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程详情 开始 -->
    <section class="container">
      <section class="path-wrap txtOf hLh30">
        <a href="/" title class="c-999 fsize14">首页</a>
        \
        <a href="/course" title class="c-999 fsize14">课程列表</a>
        \
        <span class="c-333 fsize14">{{ course.title }}</span>
      </section>
      <div>
        <article class="c-v-pic-wrap" style="height: 357px">
          <section class="p-h-video-box" id="videoPlay">
            <img :src="course.cover" :alt="course.title" class="dis c-v-pic" />
          </section>
        </article>
        <aside class="c-attr-wrap">
          <section class="ml20 mr15">
            <h2 class="hLh30 txtOf mt15">
              <span class="c-fff fsize24">{{ course.title }}</span>
            </h2>
            <section class="c-attr-jg">
              <span class="c-fff">价格：</span>
              <b class="c-yellow" style="font-size: 24px">{{ course.price }}</b>
            </section>
            <section class="c-attr-mt c-attr-undis">
              <span class="c-fff fsize14"
                >主讲： {{ course.teacherName }}&nbsp;&nbsp;&nbsp;</span
              >
            </section>
            <section class="c-attr-mt of">
              <span class="ml10 vam">
                <em class="icon18 scIcon"></em>
                <a class="c-fff vam" title="收藏" href="#">收藏</a>
              </span>
            </section>
            <section class="c-attr-mt">
              <!--
                如果当前用户没有登录则显示立即购买;如果当课程价格为0，则显示立即观看;
                如果当前用户没有登录则显示立即购买；如果当课程价格为0，则显示立即观看；
                已登录用户没有购买记录则显不山即购大Hn Sn
                已登录用户没有购买记录则显不山即购大HN Sn
               -->
              <a
                v-if="isBought || course.price === 0"
                href="#"
                title="立即观看"
                class="comm-btn c-btn-3"
                >立即观看</a
              >
              <a
                v-else
                href="javascript:;"
                title="立即购买"
                class="comm-btn c-btn-3"
                @click="buyCourse"
                >立即购买</a
              >
            </section>
          </section>
        </aside>
        <aside class="thr-attr-box">
          <ol class="thr-attr-ol clearfix">
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">购买数</span>
                <br />
                <h6 class="c-fff f-fM mt10">{{ course.buyCount }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">课时数</span>
                <br />
                <h6 class="c-fff f-fM mt10">{{ course.lessonNum }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">浏览数</span>
                <br />
                <h6 class="c-fff f-fM mt10">{{ course.viewCount }}</h6>
              </aside>
            </li>
          </ol>
        </aside>
        <div class="clear"></div>
      </div>
      <!-- /课程封面介绍 -->
      <div class="mt20 c-infor-box">
        <article class="fl col-7">
          <section class="mr30">
            <div class="i-box">
              <div>
                <section id="c-i-tabTitle" class="c-infor-tabTitle c-tab-title">
                  <a name="c-i" class="current" title="课程详情">课程详情</a>
                </section>
              </div>
              <article class="ml10 mr10 pt20">
                <div>
                  <h6 class="c-i-content c-infor-title">
                    <span>课程介绍</span>
                  </h6>
                  <div class="course-txt-body-wrap">
                    <section class="course-txt-body">
                      <div v-html="course.description"></div>
                    </section>
                  </div>
                </div>
                <!-- /课程介绍 -->
                <div class="mt50">
                  <h6 class="c-g-content c-infor-title">
                    <span>课程大纲</span>
                  </h6>
                  <section class="mt20">
                    <div class="lh-menu-wrap">
                      <menu id="lh-menu" class="lh-menu mt10 mr10">
                        <ul>
                          <!-- 文件目录 -->
                          <li
                            class="lh-menu-stair"
                            v-for="chapter in chapterList"
                            :key="chapter.id"
                          >
                            <a
                              href="javascript: void(0)"
                              :title="chapter.title"
                              class="current-1"
                            >
                              <em class="lh-menu-i-1 icon18 mr10"></em
                              >{{ chapter.title }}
                            </a>
                            <!-- 如果改小节是免费课程不管是否登录都可以看
                            如果该小节是收费课程，则必须购买后才允许查看-->
                            <ol class="lh-menu-ol" style="display: block">
                              <li
                                class="lh-menu-second ml30"
                                v-for="video in chapter.children"
                                :key="video.id"
                              >
                                <!--  -->
                                <a
                                  :href="`/play/${video.videoSourceId}`"
                                  title
                                  v-if="!isBought && video.isFree === 1"
                                >
                                  <span class="fr">
                                    <i class="free-icon vam mr10">免费试听</i>
                                  </span>
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em
                                  >{{ video.title }}
                                </a>

                                <!--  -->
                                <a
                                  :href="`/play/${video.videoSourceId}`"
                                  title
                                  v-else-if="isBought"
                                >
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em
                                  >{{ video.title }}
                                </a>
                                <!--  -->
                                <a href="javascript:;" title v-else>
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em
                                  >{{ video.title }}
                                </a>
                              </li>
                            </ol>
                          </li>
                        </ul>
                      </menu>
                    </div>
                  </section>
                </div>
                <!-- /课程大纲 -->
              </article>
            </div>
          </section>
        </article>
        <aside class="fl col-3">
          <div class="i-box">
            <div>
              <section class="c-infor-tabTitle c-tab-title">
                <a title href="javascript:void(0)">主讲讲师</a>
              </section>
              <section class="stud-act-list">
                <ul style="height: auto">
                  <li>
                    <div class="u-face">
                      <a :href="`/teacher/${course.teacherId}`">
                        <img
                          :src="course.teacherAvatar"
                          width="50"
                          height="50"
                          :alt="course.teacherName"
                        />
                      </a>
                    </div>
                    <section class="hLh30 txtOf">
                      <a
                        class="c-333 fsize16 fl"
                        :href="`/teacher/${course.teacherId}`"
                        >{{ course.teacherName }}</a
                      >
                    </section>
                    <section class="hLh20 txtOf">
                      <span class="c-999">{{ course.teacherCareer }}</span>
                    </section>
                  </li>
                </ul>
              </section>
            </div>
          </div>
        </aside>
        <div class="clear"></div>
      </div>
      <!-- 课程评论 -->
      <div class="mt50 commentHtml">
        <div>
          <h6 class="c-c-content c-infor-title" id="i-art-comment">
            <span class="commentTitle">课程评论</span>
          </h6>
          <section class="lh-bj-list pr mt20 replyhtml">
            <ul>
              <li class="unBr">
                <aside class="noter-pic">
                  <img
                    width="50"
                    height="50"
                    class="picImg"
                    src="~/assets/img/loading.gif"
                  />
                </aside>
                <div class="of">
                  <section class="n-reply-wrap">
                    <fieldset>
                      <textarea
                        v-model="comment.content"
                        placeholder="输入您要评论的文字"
                        id="commentContent"
                      ></textarea>
                    </fieldset>
                    <p class="of mt5 tar pl10 pr10">
                      <span class="fl"
                        ><tt
                          class="c-red commentContentmeg"
                          style="display: none"
                        ></tt
                      ></span>
                      <input
                        type="button"
                        value="评论"
                        class="lh-reply-btn"
                        @click="submitComment"
                      />
                    </p>
                  </section>
                </div>
              </li>
            </ul>
          </section>
          <section class="">
            <section class="question-list lh-bj-list pr">
              <ul class="pr10">
                <li v-for="comment in data.items" :key="comment.id">
                  <aside class="noter-pic">
                    <img
                      width="50"
                      height="50"
                      class="picImg"
                      :src="comment.avatar"
                    />
                  </aside>
                  <div class="of">
                    <span class="fl">
                      <font class="fsize12 c-blue">
                        {{ comment.nickname }}</font
                      >
                      <font class="fsize12 c-999 ml5">评论：</font>
                    </span>
                  </div>
                  <div class="noter-txt mt5">
                    <p>{{ comment.content }}</p>
                  </div>

                  <div class="of mt5">
                    <span class="fr"
                      ><font class="fsize12 c-999 ml5">{{
                        comment.gmtCreate
                      }}</font></span
                    >
                  </div>
                </li>
              </ul>
            </section>
          </section>
          <!-- 公共分页 开始 -->
          <div>
            <div class="paging">
              <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
              <a
                href=""
                :class="{ undisable: !data.hasPrevPages }"
                title="首页"
                @click.prevent="gotoPage(1)"
                >首页</a
              >
              <a
                href=""
                :class="{ undisable: !data.hasPrevPages }"
                title="前一页"
                @click.prevent="gotoPage(data.pageIndex - 1)"
                >&lt;</a
              >
              <a
                href=""
                :title="`第${page}页`"
                v-for="page in data.totalPages"
                :key="page"
                :class="{
                  current: data.pageIndex === page,
                  undisable: data.pageIndex === page,
                }"
                @click.prevent="gotoPage(page)"
                >{{ page }}</a
              >
              <a
                href=""
                :class="{ undisable: !data.hasNextPages }"
                title="后一页"
                @click.prevent="gotoPage(data.pageIndex + 1)"
                >&gt;</a
              >
              <a
                href=""
                :class="{ undisable: !data.hasNextPages }"
                title="末页"
                @click.prevent="gotoPage(data.totalPages)"
                >末页</a
              >
              <div class="clear"></div>
            </div>
          </div>
          <!-- 公共分页 结束 -->
        </div>
      </div>
    </section>
    <!-- /课程详情 结束 -->
  </div>
</template>

<script>
import courseApi from "/api/course";
import orderApi from "/api/order";
import cookie from "js-cookie";
import commentApi from "/api/comment";
export default {
  asyncData({ params, error }) {
    //  调用API根据id获取课程详情
    return courseApi.getCourseDatatil(params.id).then((res) => {
      return {
        course: res.data.courseDto,
        chapterList: res.data.chapterTreeList,
      };
    });
  },
  data() {
    return {
      isBought: false, //表示是否购买课程
      page: 1, //
      limit: 4, //
      data: {}, //课程分页数据
      comment: {
        courseId: "",
        teacherId: "",
        content: "",
      },
    };
  },
  methods: {
    // 立即购买
    buyCourse() {
      // 判断用户是否登录，如果未登录则提示登录
      const token = cookie.get("member_token");
      if (token) {
        //  跳转至订单页面
        this.$router.push(`/order/${this.course.id}`);
      } else {
        //  提示登录
        this.$alert("如果要购买该课程，请登录", "温馨提示", {
          confirmButtonText: "确定",
          callback: (action) => {
            if (action === "confirm") {
              //  跳转到登录
              this.$router.push("/login");
            }
          },
        });
      }
    },
    // 初始化评论列表
    initCommnetList() {
      commentApi
        .getPagedList(this.page, this.limit, this.course.id)
        .then((res) => {
          this.data = res.data;
        });
    },
    //课程分页
    gotoPage(page = 1) {
      // 调用分页API接口获取课程数据
      // 进行页码边界值判断
      if (page <= 0) {
        return;
      }
      if (page > this.data.totalPages) {
        return;
      }
      commentApi.getPagedList(page, this.limit, this.course.id).then((res) => {
        //   console.log(res);
        this.data = res.data;
      });
    },
    // 添加评论
    submitComment() {
      // 判断用户是否登录，没有登录跳转到登录页面并提示
      const token = cookie.get("member_token");
      if (token) {
        // 登录了实现评论功能
        this.addComment();
      } else {
        //未登录
        this.$message({
          type: "error",
          message: "请先登录在进行下一步操作！",
        });
        this.$router.push("/login");
      }
    },
    // 添加评论
    addComment() {
      // 判断用户是否输入内容
      if (this.comment.content.length === 0) {
        this.$message({
          type: "warning",
          message: "请输入内容",
        });
        return;
      }
      // 获取课程id
      this.comment.courseId = this.course.id;
      // 获取讲师id
      this.comment.teacherId = this.course.teacherId;
      // 调用接口添加评论
      commentApi.addComment(this.comment).then((res) => {
        // 提示评论成功
        this.$message({
          type: "success",
          message: "评论成功",
        });
        // 刷新数据
        this.initCommnetList();
        // 清空输入框的内容
        this.comment.content = "";
      });
    },
  },
  created() {
    // 通过cookie判断用户是否登录
    const token = cookie.get("member_token");
    if (token) {
      // 调用API获取该用户当前课程购买状态
      orderApi.isBought(this.$route.params.id).then((res) => {
        this.isBought = res.data;
      });
    }
    this.initCommnetList();
  },
};
</script>
